package com.test.volatileDome;


/**
 * 1.验证volstile不保证原子性
 *   1.1 假如int num = 0；num变量之前有volatile关键字修饰
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/14
 */
public class VolatileDemo1 {

    public static void main(String[] args) {
        MyData1 myData = new MyData1();

        for (int i = 1 ; i<= 20 ; i++){
            new Thread(() -> {
                    for (int j = 1 ; j<= 1000 ; j++){
                        myData.add();
                    }
            },i+"").start();
        }

        while (Thread.activeCount() > 2){
            Thread.yield();
        }

        System.out.println(Thread.currentThread().getName() + "\t num : " + myData.num);
    }
}

class MyData1{

    volatile int  num = 0;

    public void add(){
        num ++;
    }
}
